package com.qf.utils;

import com.qf.entity.Gobang;
import com.qf.entity.Room;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RegretAndReopenUntils {
    private static Map<Room,Object> stepMap=new ConcurrentHashMap<>();

    /**
     * 储存步数
     * @param room
     * @param oneStep
     * @return
     */
    public static int setStep(Room room, Integer[] oneStep){
        List GoStep = (List) stepMap.get(room);
        if(GoStep==null){
            GoStep=new ArrayList<Integer[]>();
        }
        GoStep.add(oneStep);
        stepMap.put(room,GoStep);
        return 1;
    }


    /**
     * 获取删除后的步数
     * @param moveStep
     * @return
     */
    public static List getStep(Room room,int moveStep){
        List GoStep = (List) stepMap.get(room);
        int size;
        if(GoStep==null){
            return new ArrayList();
        }
        size=GoStep.size();
        if(moveStep>=GoStep.size()){
            for(int i=0;i<size;i++){
                Integer[] step=(Integer[]) GoStep.get(GoStep.size()-1);
                room.getGobang().reMoveStep(step[0],step[1]);
                GoStep.remove(GoStep.size()-1);

            }
            stepMap.put(room,GoStep);
            return GoStep;
        }else {
            for(int i=0;i<moveStep;i++){
                Integer[] step=(Integer[]) GoStep.get(GoStep.size()-1);
                room.getGobang().reMoveStep(step[0],step[1]);
                GoStep.remove(GoStep.size()-1);
            }
            stepMap.put(room,GoStep);
            return GoStep;
        }
    }

    public static int clearStep(Room room) {
        List GoStep = (List) stepMap.get(room);
        if(GoStep==null){
            return 1;
        }
        getStep(room,GoStep.size());
        return 1;
    }
}
